package me.like.diandian.fragment;

import android.widget.LinearLayout;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentTransaction;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import me.like.diandian.R;
import me.like.diandian.base.BaseFragment;
import me.like.diandian.databinding.FragmentTab2Binding;
import me.like.diandian.util.RxUtils;

import static me.like.diandian.util.CommonUtils.getScreenWidth;

/**
 * Created by shousakai on 2019/6/21.
 */

public class Tab2Fragment extends BaseFragment<FragmentTab2Binding> {
    private FragmentTransaction ft;
    private DaiKuanListFragment fragment1,fragment2,fragment3;
    int curIndex = 0;
    private int itemWidth;
    @Override
    protected void initView() {
        binding.tlvTitle.setTitle(getString(R.string.tab2_title));
        binding.tlvTitle.setBackgroundColor(ActivityCompat.getColor(activity, R.color.white));
        itemWidth = getScreenWidth(activity) / 3;
        setTabSelect(curIndex);
        onClick(binding.tvTab1, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                setTabSelect(0);
            }
        });
        onClick(binding.tvTab2, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                setTabSelect(1);
            }
        });
        onClick(binding.tvTab3, new Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                setTabSelect(2);
            }
        });
    }

    private void setTabSelect(int index) {

        ft = activity.getSupportFragmentManager().beginTransaction();
        hideFragments(ft);
        curIndex = index;
        switch (index) {
            case 0:
                if (fragment1 == null) {
                    fragment1 = DaiKuanListFragment.getInstance(2);
                    ft.add(R.id.fragment1, fragment1);
                } else {
                    ft.show(fragment1);
                }
                break;
            case 1:
                if (fragment2 == null) {
                    fragment2 =  DaiKuanListFragment.getInstance(3);;
                    ft.add(R.id.fragment1, fragment2);
                } else {
                    ft.show(fragment2);
                }


                break;
            case 2:
                if (fragment3 == null) {
                    fragment3 =  DaiKuanListFragment.getInstance(1);
                    ft.add(R.id.fragment1, fragment3);
                } else {
                    ft.show(fragment3);
                }


                break;
            default:
                break;

        }
        setLeftMargin(index,itemWidth);
        ft.commitAllowingStateLoss();

    }

    public void setCurent(int tab){
        this.curIndex = tab;
    }


    public void setCurent2(int tab){
        this.curIndex = tab;
        setTabSelect(tab);
    }

    private void setLeftMargin(int i, int itemWidth) {
        if(binding.viewBottom.getWidth()==0){
            binding.viewBottom.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(binding.viewBottom.getWidth()==0){
                        setLeftMargin(i,itemWidth);
                    }else {
                        setLeft(i, itemWidth);
                    }
                }
            },100);

        }else {
            setLeft(i, itemWidth);
        }


    }
    private void hideFragments(FragmentTransaction ft) {
     if(fragment1!=null){
         ft.hide(fragment1);
     }
        if(fragment2!=null){
            ft.hide(fragment2);
        }
        if(fragment3!=null){
            ft.hide(fragment3);
        }
    }
    private void setLeft(int i, int itemWidth) {
        int leftMargin = i * itemWidth + ((itemWidth - binding.viewBottom.getWidth()) / 2);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) binding.viewBottom.getLayoutParams();
        layoutParams.leftMargin = leftMargin;
        binding.viewBottom.setLayoutParams(layoutParams);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab2;
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        RxUtils.delayDo(100, TimeUnit.MILLISECONDS,(result, data) -> {
            setTabSelect(curIndex);
        });
    }

}
